package com.ekuaitu.kuaitustaff.activity;


import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.ekuaitu.kuaitustaff.R;
import com.ekuaitu.kuaitustaff.api.ApiService;
import com.ekuaitu.kuaitustaff.api.ApiStore;
import com.ekuaitu.kuaitustaff.api.UrlConstant;
import com.ekuaitu.kuaitustaff.base.BaseActivity;
import com.ekuaitu.kuaitustaff.utils.CheckPermissionUtils;
import com.ekuaitu.kuaitustaff.utils.ToastUtils;
import com.uuzuche.lib_zxing.activity.CodeUtils;

import java.util.HashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import butterknife.BindView;
import butterknife.OnClick;

public class CarScanActivity extends BaseActivity {

    @BindView(R.id.edt_license)
    EditText edt_license;
    @BindView(R.id.edt_text)
    EditText edt_text;
    @BindView(R.id.tv_code)
    TextView tv_code;

    private Context mContext = this;
    private Intent intent;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_scancode;
    }

    @Override
    protected void initView() {


    }

    @Override
    protected void initData() {
        initPermission();

    }

    /**
     * 初始化权限事件
     */
    private void initPermission() {
        //检查权限
        String[] permissions = CheckPermissionUtils.checkPermission(this);
        if (permissions.length == 0) {
            //权限都申请了
            //是否登录
        } else {
            //申请权限
            ActivityCompat.requestPermissions(this, permissions, 100);
        }
    }

    @OnClick({R.id.title_back, R.id.tv_sure, R.id.tv_scan_code, R.id.tv_scan_license, R.id.tv_scan_num})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.title_back:
                this.finish();
                break;
            case R.id.tv_scan_code:
                intent = new Intent(getApplication(), CaptureNewActivity.class);
                startActivityForResult(intent, 111);
                break;

            case R.id.tv_scan_license:
                intent = new Intent(mContext, RecogCarTextActivity.class);
                intent.putExtra("ocr_type",0);
                startActivityForResult(intent, 1);
                break;
            case R.id.tv_scan_num:
                intent = new Intent(mContext, RecogCarTextActivity.class);
                intent.putExtra("ocr_type",1);
                startActivityForResult(intent, 1);
                break;
            case R.id.tv_sure:
                if (TextUtils.isEmpty(edt_license.getText().toString()) || TextUtils.isEmpty(tv_code.getText().toString()) || TextUtils.isEmpty(edt_text.getText().toString())) {
                    ToastUtils.makeText(mContext, "车牌号或二维码或文字信息不能为空", ToastUtils.LENGTH_SHORT).show();
                } else {


                    Pattern p = Pattern.compile("[0-9]*");

                    Matcher m = p.matcher(edt_text.getText().toString());

                    if (m.matches()) {

                        carBinding();
                    } else {
                        ToastUtils.makeText(mContext, "数字识别内容必须为数字", ToastUtils.LENGTH_SHORT).show();

                    }

                    break;

                }
        }}

    private void carBinding() {
        showLoadingDialog();
        HashMap<String, String> eventMap = new HashMap<>();
        new ApiStore().getApiScanCodeService(UrlConstant.Request.BASE_URL_NEW, "https://col-api.mydadao.com/parse/classes/carBinding", edt_license.getText().toString(), tv_code.getText().toString(), edt_text.getText().toString(), eventMap, new ApiStore.ParseCallBack() {
            @Override
            public void onSaveSuccess(ApiService apiService) {
                dismissLoadingDialog();
                ToastUtils.makeText(mContext, "提交成功", ToastUtils.LENGTH_SHORT).show();
                edt_license.getText().clear();
                tv_code.setText("");
                edt_text.setText("");
            }

            @Override
            public void onSaveFailure(String error) {
                dismissLoadingDialog();
                ToastUtils.makeText(mContext, "提交失败", ToastUtils.LENGTH_SHORT).show();

            }
        });
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        dismissLoadingDialog();
        if (requestCode == 1) {
            if (resultCode == 10) {
                if (data != null) {
                    edt_license.setText(data.getStringExtra("License"));
                }
            }
            if (resultCode == 11) {
                if (data != null) {
                    edt_text.setText(data.getStringExtra("Text"));
                }
            }

        }
        /**
         * 处理二维码扫描结果
         */
        if (requestCode == 111) {
            //处理扫描结果（在界面上显示）
            if (null != data) {
                Bundle bundle = data.getExtras();
                if (bundle == null) {
                    return;
                }
                if (bundle.getInt(CodeUtils.RESULT_TYPE) == CodeUtils.RESULT_SUCCESS) {
                    String result = bundle.getString(CodeUtils.RESULT_STRING);
                    //   Toast.makeText(this, "解析结果:" + result, Toast.LENGTH_LONG).show();
                    tv_code.setText(result);
                } else if (bundle.getInt(CodeUtils.RESULT_TYPE) == CodeUtils.RESULT_FAILED) {
                    Toast.makeText(mContext, "解析二维码失败", Toast.LENGTH_LONG).show();
                }
            }
        }

    }

}
